import proFetch from '../../../../hrpub/common/utils/project-fetch';
import {promptBox,toast} from 'nc-lightapp-front';



export default class BtnAction {
    constructor(comp){
        this.comp = comp
        
    }

    onButtonClick =(props, btn) => {
        const {action} = this.comp
        const {main} = props
        switch(btn){
            case 'add': 
                props.form.setFormStatus('handitemcard','edit')
                props.form.setFormItemsValue('handitemcard',{
                    'pk_itemtype':{'value': main.pk_itemtype,'display':main.title},
                    'type':{'value':main.type},
                    'enablestate':{'value':'2'} 
                })
                props.dispatch({
                    type: 'main/update',
                    payload:{
                        operation : 'add',
                        modaltitle : main.json['gx6008-000049']
                    }
                })
                props.modal.show('addModal')
                break;
            case 'delete':
                let data = props.editTable.getAllRows('handitemgrid')
                let pkStr = []
                if(data.length>0){
                    data.forEach((item) => {
                        if(item.selected){
                            pkStr.push(item.values.pk_handitem.value)  
                        }
                    });
                }
                if(pkStr.length === 0){
                    toast({ color: 'warning', content: main.json['gx6008-000016']});
                }else{
                    promptBox(
                        {
                            color: "warning",
                            title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                            content: main.json['gx6008-000019'], /* 国际化处理： 确定删除所选数据*/
                            beSureBtnClick: function () {
                                props.dispatch({
                                    type: 'main/delTableData',
                                    payload: {
                                       pks:pkStr.join()
                                    }
                                }).then((res)=>{
                                    action.tempAction.getTableData(main.pk_itemtype,main.type,main.pageInfo)
                                })
                            }
                        })
                }
                
                break;
            default :
                break;
            
        }

    }

    
    beSureBtnClick =()=>{
        const {props,action} = this.comp
        const {main} = props
        let data = {
            'handitemcard' : props.form.getAllFormValue('handitemcard')
        }
        //校验必填项 
        if(props.form.isCheckNow('handitemcard')){
            let code = props.form.getFormItemsValue('handitemcard','code').value
            let RegExp = /['$#*()!%^~@]/
            if(RegExp.test(code)){
                toast({color: 'warning',content: main.json['gx6008-000284']})
                props.modal.show('addModal')
                return
            }
            if(props.form.getFormItemsValue('handitemcard','enablestate').value === '3' && main.operation === 'edit'){
                let _this = this
                props.modal.show('addModal')
                promptBox(
                    {
                        color: "warning",
                        title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                        content: main.json['gx6008-000050'], /* 国际化处理： 确定停用？*/
                        beSureBtnClick: function () {
                           _this.saveItem(data)
                        }
                    }
                )
            }else{
                this.saveItem(data)
            }   
        }else{
            props.modal.show('addModal')
        }
    }

    saveItem =(data)=>{
        const {props,action} = this.comp
        const {main} = props
        proFetch({ 
            url: '/nccloud/hrtrn/handitem/HandItemSaveAction.do',
            body: data
        }).then((res)=>{
            props.form.EmptyAllFormValue('handitemcard')
            props.modal.close('addModal')
            action.tempAction.getTableData(main.pk_itemtype,main.type,main.pageInfo)
        })
    }

    
    cancelBtnClick =()=>{
        const {props} = this.comp
        props.dispatch({
            type: 'main/update',
            payload: {
                operation : 'browse'
            } 
        })
        console.log('关闭窗口')
        props.form.setFormStatus('handitemcard','browse')
        props.form.EmptyAllFormValue('handitemcard')
        props.modal.close('addModal')
    }


     //每页显示条数
    pageSizeSelect = (val) => {
        const {props,action} = this.comp
        const {main} = props
        main.pageInfo.pageSize = val;
        main.pageInfo.pageIndex = 0;
        props.dispatch({
            type: 'main/update',
            payload: {
                pageInfo: main.pageInfo
            }
        })
        action.tempAction.getTableData(main.pk_itemtype,main.type,main.pageInfo)
        
    }

    //分页事件
    paginationEve = (key) => {
        const {props,action} = this.comp
        const {main} = props
        main.pageInfo.pageIndex = key;
        props.dispatch({
            type: 'main/update',
            payload: {
                pageInfo: main.pageInfo
            }
        })
        action.tempAction.getTableData(main.pk_itemtype,main.type,main.pageInfo)
    };

}